﻿using System;
using HalconDotNet;
using NDK.Utils;
namespace NDK.Module.Model
{
    class HalconEngine
    {
        private static log4net.ILog m_Logger = log4net.LogManager.GetLogger("VisionDeviceModel");
        private string m_DeviceName;
        public HDevEngine MyEngine;
        HDevProcedure Procedure;
        public HDevProcedureCall ProcTrain;
        public string DeviceName
        {
            get { return m_DeviceName; }
            set { m_DeviceName = value; }
        }

        public HalconEngine()
        {
            try
            {
                MyEngine = new HDevEngine();
            }
            catch (HDevEngineException hex)
            {
                DisplayException(hex);
                m_Logger.Error(hex.StackTrace);
            }
        }
        public HalconEngine(string Halocnprojectname)
        {
            try
            {

                //IntilCamerafile();
                MyEngine = new HDevEngine();
                Procedure = new HDevProcedure(Halocnprojectname);
                ProcTrain = new HDevProcedureCall(Procedure);

            }
            catch (HDevEngineException Ex)
            {
                DisplayException(Ex);
                m_Logger.Error(Ex.StackTrace);
            }
        }
        public void Dispose()
        {
            MyEngine.Dispose();
            Procedure.Dispose();
            ProcTrain.Dispose();
        }
        public int IntilCamerafile()
        {
            int ret = 0;
            try
            {
                MyEngine = new HDevEngine();
                // No automatic operator parallelization with application-level threading
                HOperatorSet.SetSystem("parallelize_operators", "true");
                string halconExamples = FileUtils.GetAppDirectory();

                string ProcedurePath = "";

                if (Common.isFileSetPassCode == false)
                {
                     ProcedurePath = halconExamples + @"\hdevengine\procedures";
                }
                else
                {                   
                     ProcedurePath = halconExamples + @"\hdevengine\Procedures.{2559a1f2-21d7-11d4-bdaf-00c04f60b9f0}";
                }
       
                if (!HalconAPI.isWindows)
                {
                    // Unix-based systems (Mono)
                    ProcedurePath = ProcedurePath.Replace('\\', '/');
                }
                // set path of external procedures
                MyEngine.SetProcedurePath(ProcedurePath);
                Common.ProcedurePath = ProcedurePath;
            }
            catch (HDevEngineException Ex)
            {
                DisplayException(Ex);
                m_Logger.Error(Ex.StackTrace);
                ret = 1;
            }
            return ret;
        }
        private void DisplayException(HDevEngineException Ex)
        {
            string FullMessage = "Message: <" + Ex.StackTrace + ">" +
                ",  Error in program / procedure: <" + Ex.ProcedureName + ">" +
                ",  program line: <" + Ex.LineText + ">" +
                ",  line number: <" + Ex.LineNumber + ">" +
                ",  HALCON Error Number: <" + Ex.HalconError + ">";

            m_Logger.Error(FullMessage);

        }
        public int LoadHalconprojectfile(string Halocnprojectname)
        {
            int ret = 0;
            try
            {
                MyEngine = new HDevEngine();
                Procedure = new HDevProcedure(Halocnprojectname);
                ProcTrain = new HDevProcedureCall(Procedure);
            }
            catch (HDevEngineException Ex)
            {
                DisplayException(Ex);
                m_Logger.Error(Ex.StackTrace);
                ret = 1;
            }
            return ret;
        }

        public int ExcuteHalconprojectfile()
        {
            int ret = 0;
            try
            {
                ProcTrain.Execute();
            }
            catch (HDevEngineException Ex)
            {
                DisplayException(Ex);
                Console.WriteLine(Ex);
                m_Logger.Error(Ex.StackTrace);
                ret = 1;
                throw Ex;
            }
            return ret;
        }
    }
}
